﻿var SCA = SCA || {};
SCA.UI = SCA.UI || {};

SCA.UI.Main = function()
{

    this.siteSettingsTable = new SCA.UI.SiteSettings.Table();
    this.siteSettingsDialog = new SCA.UI.SiteSettings.Dialog(this.siteSettingsTable);
    this.documentTemplatesTable = new SCA.UI.DocumentTemplates.Table();
    this.documentTemplatesDialog = new SCA.UI.DocumentTemplates.Dialog(this.documentTemplatesTable);
    this.documentTemplateMappingsTable = new SCA.UI.DocumentTemplatesMappings.Table();
    this.documentTemplateMappingsDialog = new SCA.UI.DocumentTemplatesMappings.Dialog(this.documentTemplateMappingsTable);
    this.explorerLocationsTable = new SCA.UI.ExplorerLocations.Table();
    this.explorerLocationsDialog = new SCA.UI.ExplorerLocations.Dialog(this.explorerLocationsTable);
    this.mappingsTable = new SCA.UI.Mappings.Table();
    this.mappingsDialog = new SCA.UI.Mappings.Dialog(this.mappingsTable);

    this.configurationDialog = new SCA.UI.Configuration.Dialog();
    this.outlookConfiguration = new SCA.UI.OutlookConfigurations();

    this.Setup = function() {
        jQuery("#tabs").tabs();
        this.HideSiteSettingsTable();
    }

    this.HideSiteSettingsTable = function ()
    {
        if (!IsValidConfigurationSelected())
        {
            jQuery("#tabs").hide();
        }
        
    }

    this.populateUI = function () {
        if (!IsValidConfigurationSelected()) {
            return;
        }

        CreateForm("SCAForm");//create form for the jQueryUI dialogs 

        GenerateTableRows(SCA.AppConfiguration.SiteSettings, this.siteSettingsTable);
        GenerateTableRows(SCA.AppConfiguration.DocumentTemplates, this.documentTemplatesTable);
        GenerateTableRows(SCA.AppConfiguration.DocumentTemplateMappings, this.documentTemplateMappingsTable);
        GenerateTableRows(SCA.AppConfiguration.ExplorerConfiguration.ExplorerLocations, this.explorerLocationsTable);
        GenerateTableRows(SCA.AppConfiguration.FolderSettings, this.mappingsTable);

        Dyna();
        
    }

    
    var CreateForm = function(formName)
    {
        if (!jQuery("#" + formName).length) {

            jQuery("<form>")
              .attr("id", formName)
              .attr("name", formName)
              .appendTo(jQuery("body"));
        }
    }

    var GenerateTableRows = function(listOfItems, table)
    {
        if (listOfItems != null) {
            var length = listOfItems.length;
            for (var i = 0; i < length; i++) {
                table.AddRow(i);
            }
        }
    }

    var IsValidConfigurationSelected = function()
    {
        return !(jQuery("select[id$='DropDownListConfigurations']").val() === "00000000-0000-0000-0000-000000000000");
    }


    var Dyna = function()
    {
        jQuery("#tree1").dynatree({
            checkbox: true,
            // Override class name for checkbox icon:
            classNames: { checkbox: "dynatree-radio" },
            selectMode: 1,
            //onActivate: function (node) {
            //    jQuery("#echoActive1").text(node.data.title);
            //},
            //onSelect: function (select, node) {
            //    // Display list of selected nodes
            //    debugger;
            //    var s = node.tree.getSelectedNodes().join(", "); //TODO: use some location to store the value
            //    //jQuery("#echoSelection1").text(s);
            //},
            onDblClick: function (node, event) {
                node.toggleSelect();
            },
            onKeydown: function (node, event) {
                if (event.which == 32) {
                    node.toggleSelect();
                    return false;
                }
            },
            // The following options are only required, if we have more than one tree on one page:
            //      initId: "treeData",
            cookieId: "dynatree-Cb1",
            idPrefix: "dynatree-Cb1-"
        });

        jQuery("#tree2").dynatree({
            checkbox: true,
            selectMode: 2,
            //onSelect: function (select, node) {
            //    // Display list of selected nodes
            //    var selNodes = node.tree.getSelectedNodes();
            //    // convert to title/key array
            //    var selKeys = jQuery.map(selNodes, function (node) {
            //        return "[" + node.data.key + "]: '" + node.data.title + "'";
            //    });
            //    jQuery("#echoSelection2").text(selKeys.join(", "));
            //},
            onClick: function (node, event) {
                // We should not toggle, if target was "checkbox", because this
                // would result in double-toggle (i.e. no toggle)
                if (node.getEventTargetType(event) == "title")
                    node.toggleSelect();
            },
            onKeydown: function (node, event) {
                if (event.which == 32) {
                    node.toggleSelect();
                    return false;
                }
            },
            // The following options are only required, if we have more than one tree on one page:
            cookieId: "dynatree-Cb2",
            idPrefix: "dynatree-Cb2-"
        });
    }

    this.Setup();
}